/**********************************************************************************
// Rock
// 
// Creation:	10 Out 2009
// Updated:		11 Out 2009
// Compiler:	Visual Studio 2008
//
// Notes:		Defines a randomly generated rock
//
**********************************************************************************/

#include "Rock.h"

/**********************************************************************************/

int Rock::RangedRand(int min, int max)
{
	// Generate random numbers in the interval [range_min, range_max). 
	// In other words, range_min <= random number < range_max
	return int(rand() / (RAND_MAX + 1.0) * (max - min) + min);
 }


/**********************************************************************************/

Rock::Rock(int w, int h, float s)
{
	width  = w;				// maximum width of rock
	height = h;				// maximum height of rock
	spin   = s;				// define natural spin
	customType = objRock;	// rock type object

	Randomize();			// randomize rock' shape
}

/**********************************************************************************/

void Rock::Randomize()
{
	srand(GetTickCount()+rand());

	int x[8];    // x position of point
	int y[8];    // y position of point

	// Define size of the quadrants
	int centerX = width/2;
	int centerY = height/2;

	int centerXp20 = int( 0.2 * centerX);  // positive 20% centerX
	int centerXn20 = int(-0.2 * centerX);  // negative 20% centerX
	int centerYp20 = int( 0.2 * centerY);  // positive 20% centerY
	int centerYn20 = int(-0.2 * centerY);  // negative 20% centerY

	// define a randomly positioned point
	// inside each quadrant
	//
	//   ---------------- -centerY
	//   | q1 | q2 | q3 |
	//   |--------------- -0.2 * centerY
	//   | q4 |    | q5 |
	//   |--------------- +0.2 * centerY
	//   | q6 | q7 | q8 |
	//   ---------------- centerY

	x[0] = RangedRand(-centerX, centerXn20);
	y[0] = RangedRand(-centerY, centerYn20);

	x[1] = RangedRand(centerXn20, centerXp20);
	y[1] = RangedRand(-centerY, centerYn20);

	x[2] = RangedRand(centerXp20, centerX);
	y[2] = RangedRand(-centerY, centerYn20);

	x[3] = RangedRand(-centerX, centerXn20);
	y[3] = RangedRand(centerXn20, centerYp20);

	x[4] = RangedRand(centerXp20, centerX);
	y[4] = RangedRand(centerXn20, centerYp20);

	x[5] = RangedRand(-centerX, centerXn20);
	y[5] = RangedRand(centerYp20, centerY);

	x[6] = RangedRand(centerXn20, centerXp20);
	y[6] = RangedRand(centerYp20, centerY);

	x[7] = RangedRand(centerXp20, centerX);
	y[7] = RangedRand(centerYp20, centerY);

	Point rockVertex[8] = 
	{	
		Point(x[0],y[0]), 
		Point(x[1],y[1]), 
		Point(x[2],y[2]), 
		Point(x[4],y[4]), 
		Point(x[7],y[7]), 
		Point(x[6],y[6]), 
		Point(x[5],y[5]), 
		Point(x[3],y[3]) 
	};

	SetVertices(rockVertex, 8);
}

/**********************************************************************************/
